//
//  HJCMainTabBarController.swift
//  新浪微博Swift
//
//  Created by 郝俊聪 on 15/5/10.
//  Copyright (c) 2015年 heima. All rights reserved.
//

import UIKit

class HJCMainTabBarController: UITabBarController{
    
    //获的tabBar属性
    @IBOutlet weak var mainTabBar: HJCMainTabBar!
    
    //定时器
    var timer : NSTimer?
    
    //home控制器
    var homeVC : UINavigationController?
    
    //上一次点击的控制器
    var preNAV : UINavigationController?

    override func viewDidLoad() {
        super.viewDidLoad()

        view.backgroundColor = UIColor.whiteColor()
        
        setClildViewControllers()
        
        //设置tabbar中间橘色按钮监听
        mainTabBar.addBtn.addTarget(self, action: "clickAddBtn", forControlEvents: UIControlEvents.TouchUpInside)
        //设置tabbar的代理
        self.delegate = self
        
        //加入定时器
        if sharedaccount != nil{
            startTimer()
        }
        
    }
    
    override func viewDidAppear(animated: Bool) {
        super.viewDidAppear(animated)

//        //加入定时器
//        if sharedaccount != nil{
//            startTimer()
//        }
    }
    
    override func viewWillDisappear(animated: Bool) {
        super.viewWillDisappear(animated)
        //关闭定时器
        if sharedaccount != nil{
            stopTimer()
        }
    }
    
    ///设置tabview的子控制器们
    func setClildViewControllers()
    {
        
        homeVC = setclildViewController("Home", title: "首页", iconName: "tabbar_home")
        
        setclildViewController("Message", title: "消息", iconName: "tabbar_message_center")
        
        setclildViewController("Discover", title: "发现", iconName: "tabbar_discover")
        
        setclildViewController("Profile", title: "我", iconName: "tabbar_profile")
        
        
    }
    
    ///单独设置某个子控制器
    func setclildViewController(sbName:String,title:String,iconName:String) ->UINavigationController
    {
        //从storyboard加载导航控制器
        let sb = UIStoryboard(name:sbName, bundle: nil)
        let nvc = sb.instantiateInitialViewController() as? UINavigationController
        
        //设置站顶控制器的标题
        nvc?.topViewController.title = title
        
        //设置tabbar
        nvc?.tabBarItem.title = title
        nvc?.tabBarItem.image = UIImage(named: iconName)
        nvc?.tabBarItem.selectedImage = UIImage(named:iconName + "_highlighted")
        
        //加入到tabbarController中
        self.addChildViewController(nvc!)
        
        return  nvc!
    }
    
    ///点击中间的按钮，发布微博
    func clickAddBtn ()
    {
        //加载SB
        let sb = UIStoryboard(name: "Compose", bundle: nil)
        let composeVC = sb.instantiateInitialViewController() as! UIViewController
        //跳转到compose控制器
        presentViewController(composeVC, animated: true) { () -> Void in
        }
    }
    
    ///开始定时器
    func startTimer(){

        //创建
        timer = NSTimer(timeInterval: 10, target: self, selector: "loadUnReadStatus", userInfo: nil, repeats: true)
//        加入运行循环
//        println("\(NSThread.currentThread())")
        NSRunLoop.currentRunLoop().addTimer(timer!, forMode: NSRunLoopCommonModes)
    
    }
    
    ///关闭定时器
    private func stopTimer(){
        //关闭定时器
        timer?.invalidate()
        //清空属性
        timer = nil
    }
    
    ///定时器方法:加载未读数据
    func loadUnReadStatus(){
        //如果没有登录，直接返回
        if sharedaccount?.uid == nil{
            return
        }
        //加载数据获得未读数据
        HJCStatus.loadUnReadStatus { (count) -> () in
            println(count)
            
           //设置homeItem的badge
            self.homeVC?.tabBarItem.badgeValue = (count != 0) ? "\(count)" : nil
           //设置icon的badge
           UIApplication.sharedApplication().applicationIconBadgeNumber = count
            
        }
    }

}

//MARK:-UITabBarControllerDelegate
///监听选择的哪个控制器
extension HJCMainTabBarController : UITabBarControllerDelegate{
    func tabBarController(tabBarController: UITabBarController, didSelectViewController viewController: UIViewController) {
        //        println(viewController)
        //如果本次点击，和上一次点击的都是homevc，就刷新数据
        if viewController == homeVC && preNAV == homeVC{
            //滚动到最上面
            let hometvc = homeVC?.topViewController as? HJCHomeTableViewController
            hometvc?.tableView.scrollToRowAtIndexPath(NSIndexPath(forRow: 0, inSection: 0), atScrollPosition: UITableViewScrollPosition.Top, animated: true)
            
        }
        //记录点击的控制器
        preNAV = viewController as? UINavigationController
    }
    
    
}
